package com.atguigu.gulimall.product.app;

import java.util.*;
import java.util.stream.Collectors;

import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrAttrgroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 属性分组
 *
 * @author zhough
 * @email zhough@gmail.com
 * @date 2024-01-22 17:20:01
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catId") Long catId) {
        PageUtils page = attrGroupService.queryPage(params, catId);


        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 根据属性组id查询关联的属性信息
     */
    @RequestMapping("/{attrGroupId}/attr/relation")
    // @RequiresPermissions("product:attrgroup:list")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        Collection<AttrEntity> data = attrGroupService.queryAttrRelation(attrGroupId);
        return R.ok().put("data", data);
    }

    /**
     * 根据类别id查询关联的属性信息
     */
    @RequestMapping("/{catelogId}/withattr")
    // @RequiresPermissions("product:attrgroup:list")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVo> data = attrGroupService.getAttrGroupWithAttrs(catelogId);
        return R.ok().put("data", data);
    }

    /**
     * 根据属性组id查询关联的属性信息
     */
    @PostMapping("/attr/relation")
    // @RequiresPermissions("product:attrgroup:list")
    public R attrRelation(@RequestBody List<AttrAttrgroupRelationVo> vos) {

        List<AttrAttrgroupRelationEntity> entityList = vos.stream().map(item -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationService.saveBatch(entityList);
        return R.ok();
    }

    /**
     * 根据属性组id查询未关联的属性信息
     */
    @RequestMapping("/{attrGroupId}/noattr/relation")
    // @RequiresPermissions("product:attrgroup:list")
    public R noAttrRelation(@PathVariable("attrGroupId") Long attrGroupId, @RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryNoAttrRelation(attrGroupId, params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        attrGroup.setCatelogPath(categoryService.getCatelogPath(attrGroup.getCatelogId()));

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 删除 [{attrId: 3, attrGroupId: 4}]
     */
    @RequestMapping("/attr/relation/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R deleteRelation(@RequestBody AttrAttrgroupRelationVo[] attrGroupIds) {
        attrGroupService.deleteRelation(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
